﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThouShaltNotBrick.Models;
using GMLogging;
using ThouShaltNotBrickLevelEditor.Code.GeneralObjects;
using System.IO;

namespace ThouShaltNotBrickLevelEditor.Code.Helpers
{
    public static class LevelDataModifier
    {
        #region Properties
        private static Comparison<BrickData> mBrickDataComparison;
        #endregion

        #region Constructors
        static LevelDataModifier()
        {
            mBrickDataComparison = new Comparison<BrickData>(
                delegate(BrickData pFirst, BrickData pSecond)
                {
                    int rowCompare = pFirst.Row.CompareTo(pSecond.Row);
                    return rowCompare != 0 ? rowCompare : pFirst.Column.CompareTo(pSecond.Column);
                });
        }
        #endregion

        #region Methods
        public static LevelData CreateEmptyLevelData()
        {
            LevelData newLevelData = new LevelData(
                string.Empty,
                new List<BrickData>());
            return newLevelData;
        }

        public static LevelData ChangeName(
            LevelData pLevelData,
            string pNewName)
        {
            if (pLevelData == null)
            {
                Exception e = new ArgumentNullException("pLevelData");
                Logger.Error(e.Message);
                throw e;
            }

            if (pNewName == null)
            {
                Exception e = new ArgumentNullException("pNewName");
                Logger.Error(e.Message);
                throw e;
            }

            List<BrickData> brickDataList = pLevelData.GetBricks();

            LevelData newLevelData = new LevelData(
                pNewName,
                brickDataList);
            return newLevelData;
        }

        public static LevelData AddBrick(
            LevelData pLevelData,
            BrickData pBrickData)
        {
            if (pLevelData == null)
            {
                Exception e = new ArgumentNullException("pLevelData");
                Logger.Error(e.Message);
                throw e;
            }

            if (pBrickData == null)
            {
                Exception e = new ArgumentNullException("pBrickData");
                Logger.Error(e.Message);
                throw e;
            }

            List<BrickData> brickDataList = pLevelData.GetBricks();
            brickDataList.RemoveAll(bd => bd.Row == pBrickData.Row && bd.Column == pBrickData.Column);
            brickDataList.Add(pBrickData);

            LevelData newLevelData = new LevelData(
                pLevelData.Name,
                brickDataList);
            return newLevelData;
        }

        public static LevelData DeleteBrick(
            LevelData pLevelData,
            BrickPosition pBrickPosition)
        {
            if (pLevelData == null)
            {
                Exception e = new ArgumentNullException("pLevelData");
                Logger.Error(e.Message);
                throw e;
            }

            if (pBrickPosition == null)
            {
                Exception e = new ArgumentNullException("pBrickPosition");
                Logger.Error(e.Message);
                throw e;
            }

            List<BrickData> brickDataList = pLevelData.GetBricks();
            brickDataList.RemoveAll(bd => bd.Row == pBrickPosition.Row && bd.Column == pBrickPosition.Column);

            LevelData newLevelData = new LevelData(
                pLevelData.Name,
                brickDataList);
            return newLevelData;
        }

        public static LevelData PrepareLevelForWrite(
            LevelData pLevelData,
            string pLevelFilePath)
        {
            if (pLevelData == null)
            {
                Exception e = new ArgumentNullException("pLevelData");
                Logger.Error(e.Message);
                throw e;
            }

            string levelName = Path.GetFileNameWithoutExtension(pLevelFilePath);

            List<BrickData> brickDataList = pLevelData.GetBricks();
            brickDataList.Sort(mBrickDataComparison);

            LevelData newLevelData = new LevelData(
                levelName,
                brickDataList);
            return newLevelData;
        }
        #endregion
    }
}